package com.kris.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.example.demoex.DemoExMainActivity;
import com.example.demoex.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SearchFlyActivity extends Activity implements OnClickListener {  
    public static String[] keywords = { "QQ", "Sodino", "APK", "GFW", "Ǧ��",//  
        "����", "���澫��", "MacBook Pro", "ƽ�����", "��ʫ����",//  
        "����ŷ TR-100", "�ʼǱ�", "SPY Mouse", "Thinkpad E40", "�������",//  
        "�ڴ�����", "��ͼ", "����", "����", "����",//  
        "ͨѶ¼", "������", "CSDN leak", "��ȫ", "3D",//  
        "��Ů", "����", "4743G", "����", "����",//  
        "ŷ��", "�����", "��ŭ��С��", "mmShow", "���׹�����",//  
        "iciba", "��ˮ��ϵ", "����App", "������", "365����",//  
        "����ʶ��", "Chrome", "Safari", "�й��Siri", "A5������",//  
        "iPhone4S", "Ħ�� ME525", "���� M9", "�῵ S2500" };  
private KeywordsFlow keywordsFlow;  
private Button btnIn, btnOut;  
private List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.search_fly_main);  
    btnIn = (Button) findViewById(R.id.button1);  
    btnOut = (Button) findViewById(R.id.button2);  
    btnIn.setOnClickListener(this);  
    btnOut.setOnClickListener(this);  
    keywordsFlow = (KeywordsFlow) findViewById(R.id.frameLayout1);  
    keywordsFlow.setDuration(800l);  
    keywordsFlow.setOnItemClickListener(this);  
    
    myData = getData();
    
    keywords = getResources().getStringArray(R.array.demo_names);
    feedKeywordsFlow(keywordsFlow, keywords);  
    keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);  
    
    //add virtual menu
    try {  
        getWindow().addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));  
    }catch (NoSuchFieldException e) {  
        // Ignore since this field won't exist in most versions of Android  
    }catch (IllegalAccessException e) {  
        Log.w("feelyou.info", "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);  
    }  
  //add end virtual menu
}  

protected List<Map<String, Object>> getData() {
	List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
	String[] titles = getResources().getStringArray(R.array.demo_names);
	String[] intents = getResources().getStringArray(R.array.demo_intents);
	for (int i = 0; i < titles.length; ++i) {
		addItem(myData, titles[i], new Intent(intents[i].toString()));
	}
    
	return myData;
}

protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
    Map<String, Object> temp = new HashMap<String, Object>();
    temp.put("title", name);
    temp.put("intent", intent);
    data.add(temp);
}

private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {  
    Random random = new Random();  
    for (int i = 0; i < KeywordsFlow.MAX; i++) {  
        int ran = random.nextInt(arr.length);  
        String tmp = arr[ran];  
        keywordsFlow.feedKeyword(tmp);  
    }  
}  

@Override  
public void onClick(View v) {  
    if (v == btnIn) {  
        keywordsFlow.rubKeywords();  
        // keywordsFlow.rubAllViews();  
        feedKeywordsFlow(keywordsFlow, keywords);  
        keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);  
    } else if (v == btnOut) {  
        keywordsFlow.rubKeywords();  
        // keywordsFlow.rubAllViews();  
        feedKeywordsFlow(keywordsFlow, keywords);  
        keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);  
    } else if (v instanceof TextView) {  
        String keyword = ((TextView) v).getText().toString();  
//        Intent intent = new Intent();  
//        intent.setAction(Intent.ACTION_VIEW);  
//        intent.addCategory(Intent.CATEGORY_DEFAULT);  
//        intent.setData(Uri.parse("http://www.google.com.hk/#q=" + keyword));  
//        startActivity(intent);
        Toast.makeText(this, keyword, Toast.LENGTH_SHORT).show();
        int placeIndex = keyword.indexOf(".");
        keyword = keyword.substring(0, placeIndex);
        Log.e("zhengpanyong", "keyword:" + keyword);
        int place = Integer.valueOf(keyword) - 1;
        
        Map<String, Object> map = (Map<String, Object>)myData.get(place);
        Intent intent = (Intent) map.get("intent");
        startActivity(intent);

        Log.e("Search", keyword);
    }  
}  

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
//    setMenuBackground();
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
	// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_change:
			startActivity(new Intent(SearchFlyActivity.this, DemoExMainActivity.class));
			finish();
			break;

		default:
			break;
		}
	
	return super.onOptionsItemSelected(item);
}
}